<?php
/**
 * 用户关注商场
 * @author Ice <iceinto@mallog.com.cn>
 */
class MarketAttentionModel extends BaseModel
{
    public function getAttr($uid)
    {
        //获取商场信息
        $map = array();
        $map['uid'] = $uid;
        $att = $this->where($map)->select();
        $this->setMalls($att);
        return $att;
    }

    /**
     * 添加一个用户关注的商场
     * @param int $uid
     * @param int $mid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function addAtt($uid, $mid)
    {
        $data = array();
        $data['uid'] = $uid;
        $data['mid'] = $mid;
        $data['create_time'] = time();
        return $this->add($data);
    }

    /**
     * 删除一个用户关注的商场
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function deleteAtt($uid, $mid)
    {
        $map = array();
        $map['uid'] = $uid;
        $map['mid'] = $mid;
        return $this->where($map)->delete();
    }

    /**
     * 根据用户和商场查询详细
     * @param int $uid 用户
     * @param int $mid 品牌
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function findAtt($uid, $mid)
    {
        $map = array();
        $map['uid'] = $uid;
        $map['mid'] = $mid;
        return $this->where($map)->find();
    }

    /**
     * 根据商场获取关注数
     * @param int $mid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function findAttCount($mid)
    {
        $map = array();
        $map['mid'] = $mid;
        return $this->where($map)->count();
    }

    /**
     * 传址修改商场信息
     * @param array $malls 商场
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function setMalls(&$malls)
    {
        $MarketModel = D('Market');
        foreach ($malls as $key => $mall) {
            $mid = $mall['mid'];
            $mall = $MarketModel->getByIdDetailAndImage($mid);
            if ($mall) {
                $malls[$key] = array_merge($malls[$key], $mall);
            } else {
                unset($malls[$key]);
            }
        }
    }

    /**
     * 单一判断商场是否已经被关注
     * @param int $uid
     * @param array $mall
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function checkUserIsAttrMall($uid, &$mall)
    {
        $att = $this->findAtt($uid, $mall['id']);
        if ($att) {
            $mall['is_att'] = 1;
        } else {
            $mall['is_att'] = 0;
        }
    }

    /**
     * 判断商场列表中是否已经被关注
     * @param int $uid
     * @param array $mall
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function checkUserIsAttrMalls($uid, &$malls)
    {
        if (is_array($malls)) {
            foreach ($malls as $key => $mall) {
                $att = $this->findAtt($uid, $mall['id']);
                if ($att) {
                    $malls[$key]['is_att'] = 1;
                } else {
                    $malls[$key]['is_att'] = 0;
                }
            }
        }
    }

    /**
     * 获取用户关注的商场数
     * @param $uid
     * @return mixed
     * @author Ice <iceinto@mallog.com.cn>
     */
    function getUserAttCount($uid)
    {
        $map = array();
        $map['uid'] = $uid;
        return $this->where($map)->count();
    }

    /**
     * 获取关注商场活动信息
     * @param $uid
     * @author Ice <iceinto@mallog.com.cn>
     */
    function getAttDis($uid)
    {
        $fields = 'm_discount.mid,m_discount.stime,m_discount.etime,m_discount.title,m_discount.intr';
        $join = 'INNER JOIN m_discount ON m_market_attention.mid = m_discount.mid';
        $map = array();
        $map['m_discount.etime'] = array('egt', $this->getThisDayTime());
        $map['m_market_attention.uid'] = $uid;
        $map['m_discount.fid'] = 0;
        $map['m_discount.bid'] = 0;
        $dis = $this->field($fields)->join($join)->where($map)->limit('0,20')->order('m_discount.stime')->select();
        if ($dis) {
            foreach ($dis as $key => $d) {
                $mfb = $this->getAdMFB($d['mid']);
                $dis[$key] = array_merge($d, $mfb);
                $dis[$key]['ad_url'] = strtolower(U('/' . $mfb['mall']['initial']));
                $dis[$key]['image_url'] = D('Market')->getMarketImage($mfb['mall']['image']);
            }
        }
        return $dis;
    }
}

?>